<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
    <th:block th:include="include :: header('新增叶子单元')"/>
</head>
<body class="white-bg">
<div class="wrapper wrapper-content animated fadeInRight ibox-content">
    <form class="form-horizontal m" id="form-leaf_unit_city-add">
        <div class="col-md-12">
            <div class="form-group" hidden id="unit3Li">
                <label class="col-sm-3 control-label">三级单元：</label>
                <div class="col-sm-8">
                    <select class="form-control"
                            id="unit3Select"
                            name="unit3Id"
                            onchange="setNextUnitSelect(this.value, 3)">
                    </select>
                </div>
            </div>
            <div class="form-group" hidden id="unit4Li">
                <label class="col-sm-3 control-label">四级单元：</label>
                <div class="col-sm-8">
                    <select class="form-control"
                            id="unit4Select"
                            name="unit4Id"
                            onchange="setNextUnitSelect(this.value, 4)">
                    </select>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">包区名称：</label>
                <div class="col-sm-8">
                    <select class="form-control"
                            id="unit5Select"
                            name="unit5Id"
                            onchange="setNextUnitSelect(this.value, 5)">
                    </select>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">社区/行政村：</label>
                <div class="col-sm-8">
                    <input class="form-control"
                           id="unit6Select"
                           name="unit6Name"
                           placeholder="请输入社区/行政村" type="text">
                </div>
            </div>
        </div>
    </form>
</div>
<th:block th:include="include :: footer"/>
<script th:inline="javascript">
    var prefix = ctx + "workbench/leaf_unit_city"
    $("#form-leaf_unit_city-add").validate({
        focusCleanup: true
    });

    $(function () {
        $.ajax({
            method: 'post',
            url: '/workbench/leaf_unit_city/getMyDept',
            success: function (dept) {
                let level = dept.level;
                if (level !== 2) {
                    setPreviousUnitSelect(dept.parentId, level);
                }
                setCurrentSelect(dept, level);
                setNextUnitSelect(dept.deptId, level);
            }
        })
    })

    /**
     * 设置上级单元选择器
     * @param parentId 当前选择的项
     * @param currentLevel 单元等级
     */
    function setPreviousUnitSelect(parentId, currentLevel) {
        $.ajax({
            method: 'post',
            url: '/system/dept/selectByDeptId',
            data: {
                deptId: parentId,
            },
            success: function (previousUnit) {
                let selectId
                let parentLevel = currentLevel - 1
                switch (parentLevel) {
                    case 5:
                        selectId = "#unit5Select"
                        setPreviousUnitSelect(previousUnit.parentId, parentLevel)
                        break
                    case 4:
                        selectId = "#unit4Select"
                        setPreviousUnitSelect(previousUnit.parentId, parentLevel)
                        break
                    case 3:
                        selectId = "#unit3Select"
                        break
                    case 2:
                        break
                    default:
                        alert("单元等级错误p:" + level)
                }
                let option = "<option value='" + previousUnit.deptId + "'>" + previousUnit.deptName + "</option>"
                $(selectId).html(option).selectpicker('refresh');
            }
        })
    }

    /**
     * 设置当前选择器的值
     */
    function setCurrentSelect(dept, level) {
        let selectId
        switch (level) {
            case 9:
                selectId = "#unit9Select"
                break
            case 8:
                selectId = "#unit8Select"
                break
            case 7:
                selectId = "#unit7Select"
                break
            case 6:
                selectId = "#unit6Select"
                break
            case 5:
                selectId = "#unit5Select"
                break
            case 4:
                selectId = "#unit4Select"
                break
            case 3:
                selectId = "#unit3Select"
                break
            case 2:
                break
            default:
                alert("单元等级错误c：" + level)
        }
        let options = "<option value='" + dept.deptId + "'>" + dept.deptName + "</option>";
        $(selectId).html(options).selectpicker('refresh');
    }

    /**
     * 设置下级单元选择器
     * @param deptId 本单元id
     * @param parentLevel 本单元等级
     */
    function setNextUnitSelect(deptId, parentLevel) {
        let currentLevel = parentLevel + 1
        $.ajax({
            method: 'post',
            url: '/system/dept/getChildrenDeptFilterByTypes',
            data: {
                deptId: deptId
            },
            success: function (list) {
                let options = "";
                for (let i = 0; i < list.length; i++) {
                    let option = list[i];
                    options += "<option value='" + option.deptId + "'>" + option.deptName + "</option>";
                }
                let liId
                let selectId
                let firstUnit = list[0]
                switch (currentLevel) {
                    case 5:
                        selectId = "#unit5Select"
                        break
                    case 4:
                        selectId = "#unit4Select"
                        liId = "#unit4Li"
                        if (firstUnit !== undefined) {
                            setNextUnitSelect(list[0].deptId, currentLevel)
                        }
                        break
                    case 3:
                        selectId = "#unit3Select"
                        liId = "#unit3Li"
                        if (firstUnit !== undefined) {
                            setNextUnitSelect(list[0].deptId, currentLevel)
                        }
                        break
                    default:
                        alert("单元等级错误n:" + level)
                }
                $(selectId).html(options).selectpicker('refresh');
                if (liId !== '') {
                    $(liId).show();
                }
            }
        })
    }

    function submitHandler() {
        if ($.validate.form()) {
            $.operate.save(prefix + "/add", $('#form-leaf_unit_city-add').serialize());
        }
    }
</script>
</body>
</html>
